package com.aurora.oa.work.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.TextView;

import com.aurora.oa.R;
import com.aurora.oa.customview.ItemDivider;
import com.aurora.oa.customview.ScrollTextView;
import com.aurora.oa.home.bean.ArticleBean;
import com.aurora.oa.work.adapter.PersonComponentAdapter;
import com.aurora.oa.work.bean.PersonComponentBean;
import com.aurora.oa.work.presenter.WorkFragmentPresenter;
import com.aurora.oa.work.presenter.WorkFragmentPresenterImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/3/14 0014.
 */
public class WorkFragment extends Fragment implements WorkFragmentView{
    private String param;
    private WorkFragmentPresenter workFragmentPresenter;
    private TextView tv_notice_titlle;
    private WebView wv_news_announce;
    private RecyclerView rv_person_component;
    private List<PersonComponentBean> personComponentDatas;
    private  String[] itemName = new String[]{
            "公告","日志","文件","审批","日常","申请","巡店日志","查看巡店"
    };
    private int[] itemImageID = new int[]{
            R.mipmap.ico_work_notice,
            R.mipmap.ico_work_log,
            R.mipmap.ico_work_file,
            R.mipmap.ico_work_apply,
            R.mipmap.ico_work_daily,
            R.mipmap.ico_work_sp,
            R.mipmap.ico_work_cjhm,
            R.mipmap.ico_work_xdrz,
    };

    Activity mAct;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mAct = getActivity();
        initData();
        View view = inflater.inflate(R.layout.fragment_work_main,container,false);
        tv_notice_titlle = (TextView) view.findViewById(R.id.tv_notice_titlle);
        wv_news_announce = (WebView) view.findViewById(R.id.wv_news_announce);
        //自定义ScrollTextView调用setScrollText()设置内容
      //  wv_news_announce.setScrollText(getString(R.string.big_text));

        rv_person_component = (RecyclerView) view.findViewById(R.id.rv_person_component);


        return view;
    }

    private void initData() {
        workFragmentPresenter = new WorkFragmentPresenterImpl(this);
        //初始化个人工作组件
        personComponentDatas = new ArrayList<>();
        for(int i=0;i<itemName.length;i++){
            personComponentDatas.add(new PersonComponentBean(itemImageID[i],itemName[i]));
        }
    }

    public static WorkFragment newInstance(String param){
        WorkFragment workFragment = new WorkFragment();
        Bundle bundle = new Bundle();
        bundle.putString("param",param);
        workFragment.setArguments(bundle);
        return workFragment;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(savedInstanceState!=null){
            param = savedInstanceState.getString("param");
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        PersonComponentAdapter personComponentAdapter = new PersonComponentAdapter(mAct,personComponentDatas);
        workFragmentPresenter.getWorkMainDataHttp();
        rv_person_component.setAdapter(personComponentAdapter);
        rv_person_component.setLayoutManager(new GridLayoutManager(mAct,4));
        rv_person_component.setItemAnimator(new DefaultItemAnimator());
        rv_person_component.addItemDecoration(new ItemDivider().setDividerWith(5).setDividerColor(Color.TRANSPARENT));
        personComponentAdapter.setOnClickListener(new PersonComponentAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                switch (itemName[position]){
                    case "报销":
                        startActivity(new Intent(mAct,WorkExpenceMainActivity.class));
                        break;
                    case "日志":
                        startActivity(new Intent(mAct,WorkLogMainActivity.class));
                        break;
                    case "申请":
                        startActivity(new Intent(mAct,ApplyMainActivity.class));
                        break;
                    case "公告":
                        startActivity(new Intent(mAct,NoticeListActivity.class));
                        break;
                    case "审批":
                        startActivity(new Intent(mAct,ApproveMainActivity.class));
                        break;
                    case "日常":
                        startActivity(new Intent(mAct,DailyMainActivity.class));
                        break;
                    case "巡店日志":
                        startActivity(new Intent(mAct,PatrollogMainActivity.class));
                        break;
                    case "查看巡店":
                        startActivity(new Intent(mAct,SeePatrolShopMainActivity.class));
                        break;
                    case "文件":
                        startActivity(new Intent(mAct,SeePatrolShopMainActivity.class));
                        break;



                }
            }
        });
    }

    @Override
    public void refreshWorkMainPage(ArticleBean articleBean) {
        tv_notice_titlle.setText(articleBean.getTitle());
      //  tv_notice_content.setScrollText(articleBean.getContent());
        Log.e("news",articleBean.getContent());
        wv_news_announce.loadDataWithBaseURL(null, articleBean.getContent(), "text/html", "utf-8", null);

    }
}
